/****************************************************************************

An ADC abstraction layer.

Author:			Jon Maloney
Last Edited:	20/03/2012

****************************************************************************/

/****************************************************************************
A special data type that represents the sample and the sample ID. 
	The entire structure is 16 bits.
	The first 12 bits are used for storing the value read from the ADC
	The last 4 bits are used to identify the source of the sample 
	i.e. the sample ID.
****************************************************************************/
typedef union
{
	unsigned int sampleAndSampleID;
	struct
	{
		unsigned ADCsample	:12;
		unsigned sampleID	:4;	//Channel sample came from.
								//		1111 = chanel 15
								//		1110 = chanel 14
								//		....
								//		0001 = chanel 1
								//		0000 = chanel 0

	}sampType;
}SampleType;


/****************************************************************************
A sample configuration structure
	The entire structure is 31 bits long.
	The first 10 bits (LSB) are dedicated to 
	specifying the required sampling frequency.
	This sampling frequency is remains constant 
	whilst the configuration is current. 
	The second sampling frequency measure is a counter.
	This counter is set or decremented by the sampling
	process. 
	The next 3 bits indicates if the sampling description 
	is in frequency or period.
	The next 4 bits (MSB) are used for 
	a sample ID. To indicate which pin to obtain the 
	sample from. 
	The last 4 bits are tell the proces how many other 
	samples of like units there are to be sampled.

	the 10 bits used to specify the sampling periodOrFrequency gives a 
	maximum possible sampling periodOrFrequency of 1024. This can be 
	in a number of possible units (kHz, Hz, ms, s, m, h). 
****************************************************************************/
typedef union
{
	unsigned long sampleConfiguration;
	struct
	{
		unsigned periodOrFrequencyConfiguration	:10;
		unsigned periodOrFrequencyCount			:10;
		unsigned unit							:3;		//Unit the sample periodOrFrequency is measured in.
														//		000 = KHz	(hertz)
														//		001 = Hz	(kilo hertz)
														//		010 = ms 	(milli seconds)
														//		011 = s		(seconds)
														//		100 = m 	(minutes)
														//		101 = h		(hour)
														//		11x = h		(hour)
		unsigned sampleID						:4;		//Channel to take sample from.
														//		1111 = chanel 15
														//		1110 = chanel 14
														//		....
														//		0001 = chanel 1
														//		0000 = chanel 0
		unsigned nbSimilarSamples 				:4;	//Number of samples in the configuration 
														//with the same unit type
														//		0	= 0;
														//		1 	= 1;
														//		....
														//		15  = 15;
										
	}sampDescription;
}SampleConfigurationType;

/****************************************************************************
A function to initialise the ADC.
****************************************************************************/
void ADC_Init(void);

/****************************************************************************
A function to Modify current sampling configuration

input:
		a sample configuration structure.

output:
		an integer indicating if the configuration 
		modification was successful
			0 	= configuration was successfull
			>0 	= an error occured.
****************************************************************************/
int modifyConfiguration(SampleConfigurationType * sampConfig);

void setupDefaultADCConfiguration(void);
